//
//  LCTableViewPickerControl.m
//  InsurancePig
//
//  Created by Leo Chang on 10/21/13.
//  Copyright (c) 2013 Good-idea Consunting Inc. All rights reserved.
//

#import "LCTableViewPickerControl.h"


#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define kNavBarHeight 40
#define cellIdentifier @"LCTableViewCell"

@interface LCTableViewPickerControl ()<UIGestureRecognizerDelegate>

@property (nonatomic, strong) NSArray *items;
@property (weak) id currentVale;
@property (nonatomic, strong) UITableView *aTableView;
@property (nonatomic, assign) CGPoint offset;
@property (nonatomic, assign) CGFloat height;
@end

@implementation LCTableViewPickerControl

- (id)initWithFrame:(CGRect)frame value:(id)value items:(NSArray *)array offset:(CGPoint)offset
{
    if (self = [super initWithFrame:frame])
    {
        if (array.count*45>=kUIScreenHeight/2.0) {
            self.height = kUIScreenHeight/2.0;
        }else{
            self.height = 45 * array.count;
        }
        
        self.backgroundColor = RGBAColor(0, 0, 0, 0.4);
        self.currentVale = value;
        self.items = [NSArray arrayWithArray:array];
        self.offset = offset;
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        tap.delegate = self;
        [self addGestureRecognizer:tap];
        [self initializeControlWithFrame:frame];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)initializeControlWithFrame:(CGRect)frame
{
    
    self.aTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kUIScreenHeight, kUIScreenWidth, self.height) style:UITableViewStylePlain];
    _aTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_aTableView setDelegate:self];
    [_aTableView setDataSource:self];
    [self addSubview:_aTableView];
    [self.aTableView registerClass:NSClassFromString(cellIdentifier) forCellReuseIdentifier:cellIdentifier];
}

- (void)showInView:(UIView *)view
{
    //add mask
//    self.maskView = [[UIView alloc] initWithFrame:view.bounds];
//    [_maskView setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0]];
//    [view insertSubview:_maskView atIndex:[_mark integerValue]];
    
    //add a Tap gesture in maskView
    
    [UIView animateWithDuration:kAnimationDuration delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
        [self.aTableView setFrame:CGRectMake(0, SCREEN_HEIGHT - self.height, kUIScreenWidth, self.height)];
    } completion:^(BOOL finished){
        //scroll to currentValue
        [UIView animateWithDuration:0.2 animations:^{
            [self.aTableView setFrame:CGRectMake(0, SCREEN_HEIGHT - self.height + 5, kUIScreenWidth, self.height)];
        } completion:^(BOOL finished){
            [UIView animateWithDuration:0.1 animations:^{
                [self.aTableView setFrame:CGRectMake(0, SCREEN_HEIGHT - self.height, kUIScreenWidth, self.height)];
            } completion:^(BOOL finished){
                //configure your settings after view animation completion
            }];
        }];

        NSInteger index = [_items indexOfObject:_currentVale];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
        [_aTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    }];
}

- (void)showInView:(UIView*)view block:(LCPickerCallback)callback
{
    [self showInView:view];
    
    self.callback = callback;
}

- (void)tap:(UITapGestureRecognizer*)sender
{
    CGPoint point = [sender locationInView:self];
    if (point.y <= kUIScreenHeight - self.height) {
        [self dismiss];
    }
    
}

- (void)dismiss
{
    //animation to dismiss
    [UIView animateWithDuration:kAnimationDuration delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
        [self.aTableView setFrame:CGRectMake(0, SCREEN_HEIGHT, kUIScreenWidth, self.height)];
    } completion:^(BOOL finished){
        [self removeFromSuperview];
    }];

}

- (void)dismissPickerView:(id)sender
{
    /*
     if you use block then comment these lines
     */
    if ([self.delegate respondsToSelector:@selector(selectControl:didSelectWithItem:)])
        [self.delegate selectControl:self didSelectWithItem:[NSString stringWithFormat:@""]];
    
    //callback with block
    if (self.callback)
        self.callback(self, @"",NO);
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_items count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LCTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.attributeItem = self.items[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.delegate respondsToSelector:@selector(selectControl:didSelectWithItem:)])
        [self.delegate selectControl:self didSelectWithItem:[_items objectAtIndex:indexPath.row]];
    
    if (self.callback)
        self.callback(self, _items[indexPath.row],YES);
}

#pragma mark tapGestureRecgnizerdelegate 解决手势冲突
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isKindOfClass:[UITableView class]]){
        return NO;
    }
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }
    return YES;
}

@end 
